package com.example.myblog1_0.controller;

import com.example.myblog1_0.mapper.UserMapper;
import com.example.myblog1_0.model.Comment;
import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.CommentService;
import com.example.myblog1_0.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: xc
 * @Date: 2019/3/12 19:25
 * @Description:
 **/
@RestController
public class CommentController {
    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;

    @GetMapping("/getCommentByArticleID")
    public JSONObject getCommentByArticleID(@RequestParam("articleID") String articleID){

        return commentService.getCommentByID(articleID);
    }
    @PostMapping("/saveComment")
    public JSONObject saveComment(@RequestParam("articleID") String articleID,@RequestParam("content") String content,@RequestParam("osName") String osName,@AuthenticationPrincipal Principal principal){
        String username = principal.getName();
        Comment comment = new Comment();
        comment.setArticleID(Integer.parseInt(articleID));
        comment.setContent(content);
        comment.setUserID(username);
        comment.setOsName(osName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sdf.format(date);
        comment.setSendTime(time);
        return commentService.saveComment(comment);
    }
    @GetMapping("/getReplyByCommentID")
    public JSONObject getReplyByCommentID(String id){
        return commentService.getReplyByCommentID(id);
    }
    @PostMapping("/saveReplyComment")
    public JSONObject saveReplyComment(@RequestParam("articleID") String articleID,@RequestParam("content") String content,@RequestParam("beReplyName")String beReplyName,@RequestParam("replyID")String replyID,@AuthenticationPrincipal Principal principal){
        try {
            String username = principal.getName();
            MyUser findUser = userService.getUserByNickname(beReplyName);
            String toReplyID = replyID.substring(10);
            Comment comment = new Comment();
            comment.setArticleID(Integer.parseInt(articleID));
            comment.setReplyID(toReplyID);
            comment.setLastUserID(findUser.getUsername());
            comment.setUserID(username);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String time = sdf.format(date);
            comment.setSendTime(time);
            comment.setContent(content);

            return commentService.saveComment(comment);
        }catch (Exception e){
            e.printStackTrace();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status",500);

            return jsonObject;

        }

    }
}
